/*     */ package com.kendoui.taglib.json;
/*     */ 
/*     */ import java.beans.BeanInfo;
/*     */ import java.beans.IntrospectionException;
/*     */ import java.beans.Introspector;
/*     */ import java.beans.PropertyDescriptor;
/*     */ import java.io.IOException;
/*     */ import java.io.StringWriter;
/*     */ import java.io.Writer;
/*     */ import java.lang.reflect.Array;
/*     */ import java.lang.reflect.InvocationTargetException;
/*     */ import java.lang.reflect.Method;
/*     */ import java.util.Calendar;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ public class Serializer
/*     */ {
/*     */   private Map<String, Object> properties(Object object)
/*     */   {
/*  26 */     Map options = new HashMap();
/*     */     try
/*     */     {
/*  29 */       for (PropertyDescriptor property : Introspector.getBeanInfo(object.getClass(), Object.class).getPropertyDescriptors()) {
/*  30 */         Method getter = property.getReadMethod();
/*     */ 
/*  32 */         if (getter != null)
/*  33 */           options.put(property.getName(), getter.invoke(object, new Object[0]));
/*     */       }
/*     */     } catch (IntrospectionException e) {
/*     */     } catch (IllegalAccessException e) {
/*     */     }
/*     */     catch (IllegalArgumentException e) {
/*     */     }
/*     */     catch (InvocationTargetException e) {
/*     */     }
/*  42 */     return options;
/*     */   }
/*     */ 
/*     */   private void quote(Writer out, String string) throws IOException {
/*  46 */     char ch = '\000';
/*     */ 
/*  48 */     out.append("\"");
/*     */ 
/*  50 */     int index = 0; for (int length = string.length(); index < length; index++) {
/*  51 */       char previous = ch;
/*  52 */       ch = string.charAt(index);
/*     */ 
/*  54 */       switch (ch) {
/*     */       case '"':
/*     */       case '\\':
/*  57 */         out.append("\\").append(ch);
/*     */ 
/*  59 */         break;
/*     */       case '/':
/*  61 */         if (previous == '<') {
/*  62 */           out.append("\\");
/*     */         }
/*  64 */         out.append(ch);
/*  65 */         break;
/*     */       case '\b':
/*  67 */         out.append("\\b");
/*  68 */         break;
/*     */       case '\n':
/*  70 */         out.append("\\n");
/*  71 */         break;
/*     */       case '\r':
/*  73 */         out.append("\\r");
/*  74 */         break;
/*     */       case '\f':
/*  76 */         out.append("\\f");
/*  77 */         break;
/*     */       case '\t':
/*  79 */         out.append("\\t");
/*  80 */         break;
/*     */       default:
/*  82 */         if ((ch < ' ') || ((ch >= '') && (ch < ' ')) || ((ch >= ' ') && (ch < '℀'))) {
/*  83 */           String hex = "000" + Integer.toHexString(ch);
/*     */ 
/*  85 */           out.append("\\u").append(hex.substring(hex.length() - 4));
/*     */         }
/*     */         else {
/*  88 */           out.append(ch);
/*     */         }
/*     */         break;
/*     */       }
/*     */     }
/*     */ 
/*  94 */     out.append("\"");
/*     */   }
/*     */ 
/*     */   public void serialize(Writer out, Object value) throws IOException {
/*  98 */     if (value == null)
/*  99 */       out.append("null");
/* 100 */     else if ((value instanceof String))
/* 101 */       quote(out, (String)value);
/* 102 */     else if ((value instanceof Number))
/* 103 */       out.append(value.toString());
/* 104 */     else if ((value instanceof Boolean))
/* 105 */       out.append(value.toString());
/* 106 */     else if ((value instanceof Date))
/* 107 */       serializeDate(out, (Date)value);
/* 108 */     else if ((value instanceof Iterable))
/* 109 */       serializeIterable(out, (Iterable)value);
/* 110 */     else if ((value instanceof Serializable))
/* 111 */       serializeMap(out, ((Serializable)value).properties());
/* 112 */     else if ((value instanceof Map))
/* 113 */       serializeMap(out, (Map)value);
/* 114 */     else if ((value instanceof Function))
/* 115 */       ((Function)value).write(out);
/* 116 */     else if (value.getClass().isArray())
/* 117 */       serializeArray(out, value);
/* 118 */     else if (value.getClass().isEnum())
/* 119 */       quote(out, value.toString());
/*     */     else
/* 121 */       serializeMap(out, properties(value));
/*     */   }
/*     */ 
/*     */   private void serializeDate(Writer out, Date value) throws IOException
/*     */   {
/* 126 */     Calendar cal = Calendar.getInstance();
/* 127 */     cal.setTime(value);
/*     */ 
/* 129 */     out.append("new Date(").append(Integer.toString(cal.get(1))).append(",").append(Integer.toString(cal.get(2))).append(",").append(Integer.toString(cal.get(5))).append(",").append(Integer.toString(cal.get(11))).append(",").append(Integer.toString(cal.get(12))).append(",").append(Integer.toString(cal.get(13))).append(",").append(Integer.toString(cal.get(14))).append(")");
/*     */   }
/*     */ 
/*     */   private void serializeMap(Writer out, Map<?, ?> map)
/*     */     throws IOException
/*     */   {
/* 147 */     out.append("{");
/*     */ 
/* 149 */     Iterator iterator = map.entrySet().iterator();
/*     */ 
/* 151 */     while (iterator.hasNext()) {
/* 152 */       Map.Entry entry = (Map.Entry)iterator.next();
/*     */ 
/* 154 */       out.append("\"").append(entry.getKey().toString()).append("\":");
/*     */ 
/* 158 */       serialize(out, entry.getValue());
/*     */ 
/* 160 */       if (iterator.hasNext()) {
/* 161 */         out.append(",");
/*     */       }
/*     */     }
/*     */ 
/* 165 */     out.append("}");
/*     */   }
/*     */ 
/*     */   private void serializeIterable(Writer out, Iterable<?> iteratable) throws IOException {
/* 169 */     out.append("[");
/*     */ 
/* 171 */     Iterator iterator = iteratable.iterator();
/*     */ 
/* 173 */     while (iterator.hasNext()) {
/* 174 */       serialize(out, iterator.next());
/*     */ 
/* 176 */       if (iterator.hasNext()) {
/* 177 */         out.append(",");
/*     */       }
/*     */     }
/*     */ 
/* 181 */     out.append("]");
/*     */   }
/*     */ 
/*     */   private void serializeArray(Writer out, Object array) throws IOException {
/* 185 */     out.append("[");
/*     */ 
/* 187 */     int index = 0; for (int length = Array.getLength(array); index < length; index++) {
/* 188 */       serialize(out, Array.get(array, index));
/*     */ 
/* 190 */       if (index < length - 1) {
/* 191 */         out.append(",");
/*     */       }
/*     */     }
/*     */ 
/* 195 */     out.append("]");
/*     */   }
/*     */ 
/*     */   public String json(Object object) throws IOException {
/* 199 */     StringWriter out = new StringWriter();
/*     */ 
/* 201 */     serialize(out, object);
/*     */ 
/* 203 */     return out.toString();
/*     */   }
/*     */ }

/* Location:           D:\Liao\kendoui.jsp.2013.1.319.trial\wrappers\jsp\kendo-taglib\kendo-taglib-2013.1.319.jar
 * Qualified Name:     com.kendoui.taglib.json.Serializer
 * JD-Core Version:    0.6.2
 */